<?php
namespace app\yunda\admin;

use app\common\builder\ZBuilder;
use app\yunda\model\Score_order;
use app\yunda\model\Danhao;
use think\Db;

/**
 * Class Category积分订单表
 * @package app\yunda\admin
 */
class ScoreOrder extends Base{
    public function index()
    {
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();
        $map['yd_score_order.delete_time']=NULL;
        $map['yd_score_order.order_type']=1;//积分订单
        $btn_1=[
            'title' => '确认发货',
            'icon'  => 'fa fa-fw fa-hand-o-up',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'data-title' => '确认要发货吗？',
            'style' => 'primary',
            'href'  => url('order_ok', ['id' => '__id__'])
        ];
//        halt($map);
        $data = Db::view('yd_score_order',true)
            ->view('yd_user',['name','post_code','home','address','phone'],'yd_user.openid=yd_score_order.openid','left')
            ->view('yd_score_goods','good_name','yd_score_goods.id=yd_score_order.good_id','left')
            ->where($map)
            ->order($order)
            ->order('id desc')
            ->paginate()->each(function($item){
                $item['address1']=$item['home'].$item['address'];
                $arr=['否','是'];
                $item['is_pay']=$arr[$item['is_pay']];
                return $item;
            });
        return ZBuilder::make('table')
            ->setTableName('yd_score_order')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['name','收件人'],
                ['phone','收件人电话'],
                ['order_id','快递单号'],
                ['good_name','兑换的商品'],
                ['address1','收件地址'],
                ['post_code','邮编'],
                ['is_pay','是否兑换'],
                ['create_time','创建时间','datetime'],
                ['right_button', '操作', 'btn']
            ])
            //->addTopButton('add', [],[])
            ->addRightButton('order_ok',$btn_1,['area'=>['400px','50%']])
            ->addTopButtons(['delete'])
            ->setRowList($data)
            ->addFilter(['name' => 'yd_user','good_name'=>'yd_score_goods'])
            ->addFilter(['is_pay'],['否','是'])
            ->setSearch(['id' => 'ID', 'order_id' => '快递单号'])
            ->fetch();
    }

    //确认发货
    public function order_ok($id=null)
    {
        if(empty($id))$this->error('参数错误');
        if($this->request->isPost()){
            $data=request()->post();
            if(empty($data['order_id']))$this->error('无可用快递单号');
            $row=Score_order::get($data['id']);
            if(!empty($row['order_id']))$this->error('该订单已发货');
            Score_order::update($data);
            Danhao::where(['order_sn'=>$data['order_id']])->update(['is_use'=>1]);
            $this->success('发货成功', null, '_parent_reload');

        }
        $info=['id'=>$id];
        // 显示编辑页面
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'id'],
                ['hidden', 'order_id',Danhao::getDanHao()],
                ['static','order_id','快递单号','',Danhao::getDanHao()],
            ])
            ->setFormData($info)
            ->setBtnTitle('submit', '确认')
            ->fetch();

    }
}